package org.onion.pattern.notify.dingtalk;

import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * xx:
 * notify:
 * dingtalk:
 * robot:
 * notify-url: https://oapi.dingtalk.com/robot/send
 * access-token: xxx
 * configs:
 * - config-id: 0
 * theme: none
 * access-token: xxx
 * - config-id: 0
 * theme: order
 * access-token: xxx
 */
@Data
@ConfigurationProperties(prefix = "onion-pattern.dingtalk.robot")
public class DingTalkRobotProperties {

    private Boolean enable = true;
    private String notifyUrl = "https://oapi.dingtalk.com/robot/send";
    private String accessToken;
    private String signKey; //加签
    private Integer executorQueueSize = 1000;
    private Integer executorThreadSize = 1;
    private List<NotifyThemeConf> configs;

    public Optional<NotifyThemeConf> findConfig(String configId, String theme) {
        if (CollectionUtils.isEmpty(configs)) {
            return Optional.empty();
        }
        for (NotifyThemeConf config : configs) {
            if (!Objects.equals(config.getConfigId(), configId)) {
                continue;
            }
            if (!Objects.equals(config.getTheme(), theme)) {
                continue;
            }
            return Optional.of(config);
        }
        return Optional.empty();
    }

    @Data
    @Accessors(chain = true)
    public static class NotifyThemeConf {
        private String configId;
        private String theme;
        private String accessToken;
        private String signKey; //加签
    }

}
